<?php
class FormController extends FrontController {
	function FormController(){
		parent::FrontController();
		$this->model = 'form';
	}
	
	public function form_item_edit(){
		if(IS_POST){
			self::form_save();
		}
		if(empty($this->id)){
			$module = 'temp';			
		}else{
			$module = self::get_val('module');
		}
		$form_name = self::get_val('form_name');
		$item_name = self::get_val('item_name');
		$item_id = self::get_val('item_id','0');
		
		$src_id = self::get_val('src_id'); //表单原始的ID
		$lang = self::get_val('lang'); //当前编辑的语言，如果无语言项，则为空
		
		$this->assign(array(
			'form_name' => $form_name,
			'id' => $this->id,
			'item_name' => $item_name,
			'item_id' => $item_id,
			'src_id' => $src_id,
			'lang' => $lang,
			'module' => $module,
		));
		$this->display();
	}
	
	private function form_save(){
		$form_id = self::get_post_val('form_name');
		if(empty($this->id)){
			$model_name = 'temp';
		}else{
			$model_name = self::get_post_val('module');
		}
		$h = new FormHelper(array(
			'name' => $form_id,
			'add_site_id' => false,
			'model_name' => $model_name,
		));
		
		$post_data = $h->save();
		self::ajax_info($post_data);
	}
	
	public function form_item_del(){
		$module = self::get_val('module');
		$form_name = self::get_val('form_name');
		$item_name = self::get_val('item_name');
		$item_id = self::get_val('item_id','0');
		$mod = &m($module);
		$mod->delete_column_by_key($this->id, $item_name . '.' . $item_id);
		self::ajax_info(Lang::get('delete_success'));
	}

	public function item_edit(){
		if(IS_POST){
			self::save_attr_item();
		}
		$code = self::get_val('code');
		$module = 'attrs';
		if(isset($_REQUEST['module'])){
			$module = self::get_request_val('module');
		}
	
		$item_name = self::get_val('name');
		if(empty($item_name)){
			$item_name = 'items';
		}
	
		$mutil_lan = self::get_int_val('mutil_lan', 1);
		$attr_mod = &m($module);
		if(!empty($code) && !empty($this->id)){
			$attr_item = $attr_mod->get_by_key($this->id, 'mutil_lan,' . $item_name . '.' . $code);
				
			$item = $attr_item[$item_name][$code];
			$item['code'] = $code;
			$langs = $this->site_info['langs'];
			if($mutil_lan == 0 || (isset($attr_item['mutil_lan']) && !$attr_item['mutil_lan'])){
				$langs = array(LANGUAGE => true);
				$this->assign('site_langs', array(LANGUAGE=>Lang::get_lan_name(LANGUAGE)));
			}
			foreach ($langs as $lang=>$is_default){
				if(isset($item[$lang]['values'])){
					$item[$lang]['values'] = implode("\n", $item[$lang]['values']);
				}
	
			}
			$type = $item['type'];
			if(isset($item[$type])){
				$this->assign('sub_trait', $item[$type]);
			}
			$this->assign('item',  $item);
		}else{
			$attr_item = $attr_mod->get_by_key_site_limit($this->id, 'mutil_lan');
			if($mutil_lan == 0 || (isset($attr_item['mutil_lan']) && !$attr_item['mutil_lan'])){
				$this->assign('site_langs', array(LANGUAGE=>Lang::get_lan_name(LANGUAGE)));
				$this->assign('mutil_lan', false);
			}
		}
	
		$relation_mod = new RelationModel();
		$this->assign('relations', $relation_mod->get_options());
	
		$form_mod = new FormsModel();
		$forms = $form_mod->get_site_forms_option(array('site_id'=>SITE_ID), 'name,title');
		$this->assign('forms', $forms);
	
		$this->assign('id', $this->id);
		$this->assign('module', $module);
		$this->assign('types', AttrsModel::get_attr_types());
		if(isset($_GET['name'])){
			$this->assign('name', $_GET['name']);
		}
		$this->display();
	}
	
	private function save_attr_item(){
		$code = self::get_post_val('item_code');
		//验证合法性
		if (!preg_match('/[[a-z|A-Z][a-z|A-Z|0-9|_]+/i', $code)) {
			self::ajax_error('代码不合法！');
		}
		$module = 'attrs';
		if(isset($_REQUEST['module'])){
			$module = self::get_request_val('module');
		} 
		$item_null_allow = false;
		if(isset($_POST['item_null_allow'])){
			$item_null_allow = true;
		}
		$mutil_lan = false;
		if(isset($_POST['is_mutil_lan']) && $_POST['is_mutil_lan'] == 1){
			$mutil_lan = true;
		}
		
		$type = self::get_post_val('type');
		$item = array(
			'required' => $item_null_allow,
			'type' => $type,
			'mutil_lan' => $mutil_lan
		);
		
		$mutil_lan = self::get_post_int_val('mutil_lan', 1);
		$attr_mod = &m($module);
		$attr = $attr_mod->get_by_key_site_limit($this->id, 'mutil_lan');
		$langs = $this->site_info['langs'];
		if($mutil_lan === 0 || (isset($attr['mutil_lan']) && !$attr['mutil_lan'])){
			$langs = array(LANGUAGE => true);
		}
		foreach ($langs as $lang=>$is_default){
			$name = self::get_post_val($lang . '_item_name');
			if(empty($name) || utf8_strlen($name)>50){
				self::ajax_error('属性名称['.Lang::get_lan_name($lang).']不正确！');
			}
			$item_default_value = self::get_post_val($lang . '_item_default_value');
			$values = explode("\n", $item_default_value);
			foreach ($values as $k=>$v){
				if(preg_match('/^\s*$/i', $v)){
					unset($values[$k]);
				}
			}
			reset($values);
			$desc = self::get_post_val($lang . '_desc');
			
			$item[$lang] = array(
				'name' => $name,
				'desc' => $desc,
			);
			if(!empty($values)){
				$item[$lang]['values'] = $values;
			}
			
		}
		
		if(empty($this->id)){
			//如果没有指定ID
			$item['code'] = $code;
			self::ajax_info('创建成功', $item);
		}
		$item_name = self::get_post_val('item_name');
		if(empty($item_name)){
			$item_name = 'items';
		}
		
		//验证代码是否存在
		if($attr_mod->exists_column(array('site_id'=>SITE_ID, '_id'=>$this->id), $item_name . '.' . $code)){
			self::ajax_error('代码已经存在：' . $code);
		}
		
		if(isset($_POST[$type . '_trait'])){
			$item['trait'] = self::get_post_val($type . '_trait');
		}
		
		if(isset($_POST[$type])){
			$item[$type] = self::get_post_val($type);
		}
		
		//追加
		$attr_mod->update_by_key($this->id, array($item_name . '.' . $code => $item));
		self::ajax_info('保存成功！');
	}
}

?>